﻿using System;
using System.Diagnostics;
using System.Numerics;

namespace ProjectEulerIssues
{
	public partial class Program
	{
		private static dynamic Problem390()
		{
			Func<long> solve = () =>
			{
				//BigInteger x = 93;
				//var x2 = x.Isqrt();

				//S^2 = (1/4)*(b^2+b^2*c^2+c^2) <= n^2
				var n = (BigInteger)Math.Pow(10, 6);
				BigInteger sum = 0;
				BigInteger max = (BigInteger)double.MaxValue;
				var sw = Stopwatch.StartNew();
				for (BigInteger b = 2; b < n; b += 2)
				{
					BigInteger bpart = (b / 2) * (b / 2);
					for (BigInteger c = b; c < n; c += 2)
					{
						var c2part = (c / 2) * (c / 2);
						var sqr = bpart * (1 + c2part * 4) + c2part;
						BigInteger s = sqr.Sqrt();
						/*BigInteger s = 0;
						if (sqr < max)
						{
							s = (BigInteger)Math.Sqrt((double)sqr);
						}
						else
						{
							s = sqr.Sqrt();
						}*/
						if (s > n)
						{
							break;
						}
						if (sqr == s * s)
						{
							Console.WriteLine("b: " + b + " c: " + c + " Cdiv4: " + (c % 4 == 0));
							//Console.WriteLine("b: " + b + " c: " + c);
							sum += s;
						}
					}
				}
				sw.Stop();
				return (long)sum;
			};
			return solve();
		}
	}
}
